package com.flightgame.hibernate;

import java.util.List;

import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

public class MessageDao {
	@Autowired
	private SessionFactory sessionFactory;
	
	@Transactional
	public void createMessage(Message message){
		try{
			sessionFactory.getCurrentSession().persist(message);
			}
		catch(Exception sq){
			sq.printStackTrace();
		}
	}
	
	public List<Message> getMessagesOfUser(User user){
		return sessionFactory.getCurrentSession().createQuery("from Message where receivername=:receivername").setParameter("receivername",user.getUsername()).list();
	}
	
	@Transactional
	public void deleteMessage(Message message){
		sessionFactory.getCurrentSession().delete(message);
	}
	
}
